/*
 * Yabumi, a 2d scrolling shoot-em-up game.
 * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 */

#include <math.h>
#include <stdbool.h>
#include "../taskmanager.h"
#include "../sprite.h"
#include "../video.h"
#include "../util.h"
#include "missile.h"
#include "flame.h"
#include "homing.h"
#include "explosion.h"
#include "smoke.h"
#include "hitcircle.h"
#include "musashi.h"

extern _list_header *active_task_list_header, *expired_task_list_header;
extern _list_header player_list_header;
_list_header musashi_list_header;

void Musashi_Init()
{
  Create_list(&musashi_list_header);
}

void Musashi_Free()
{
  Free_list(&musashi_list_header);
}

_Musashi *Musashi_Create(int x, int y, int dx, int dy, int angle)
{
  _list_item *list_item;
  list_item = (_list_item *)pmalloc(sizeof(_list_item));
  Add_item_last(&musashi_list_header, list_item);

  _Musashi *musashi = &list_item->data.Musashi;
  musashi->root = list_item;
  musashi->x = x;
  musashi->y = y;
  musashi->dx = dx;
  musashi->dy = dy;
  musashi->angle = angle;
  musashi->shield = MUSASHI_SHIELD;
  musashi->waits.fire = false;
  musashi->tasks.fire = false;
  musashi->tasks.was_hit = __hit_no;
  return musashi;
}

task Musashi_Add(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  Task_Add(Musashi_Draw, musashi, NULL, INFINITY_COUNT);
  Task_Add(Hitcircle_Add,
	   Hitcircle_Create(musashi,__hitcircle_musashi,16,__visible, 0, 0),
	   musashi, ONETIME_COUNT);
  Task_Add(Hitcircle_Add,
	   Hitcircle_Create(musashi,__hitcircle_musashi,16,__invisible, 30, 0),
	   musashi, ONETIME_COUNT);
  Task_Add(Hitcircle_Add,
	   Hitcircle_Create(musashi,__hitcircle_musashi,16,__invisible, 30, 180),
	   musashi, ONETIME_COUNT);
  Task_Add(Musashi_Move, musashi, NULL, INFINITY_COUNT);
}

task Musashi_Move(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  if (musashi->shield <= 0) {
    optv->count = KILL_COUNT;
    Task_Add(Musashi_Kill, musashi, NULL, MUSASHI_KILL_WAIT);
    return;
  };
  
  if (musashi->dx > MUSASHI_MAX_SPEED) musashi->dx = MUSASHI_MAX_SPEED;
  if (musashi->dx < -1 * MUSASHI_MAX_SPEED) musashi->dx = -1 * MUSASHI_MAX_SPEED;
  if (musashi->dy > MUSASHI_MAX_SPEED) musashi->dy = MUSASHI_MAX_SPEED;
  if (musashi->dy < -1 * MUSASHI_MAX_SPEED) musashi->dy = -1 * MUSASHI_MAX_SPEED;
  cap_angle(&musashi->angle);
  musashi->x += musashi->dx >> 8;
  musashi->y += musashi->dy >> 8;

  if (musashi->tasks.fire == true) {
    musashi->tasks.fire = false;
    if (musashi->waits.fire != true) {
      musashi->waits.fire = true;
      Task_Add(Musashi_Fire, musashi, NULL, MUSASHI_FIRE_MISSILE_WAIT);
    };
  };
  if (musashi->tasks.was_hit != __hit_no) {
    Task_Add(Musashi_was_Hit, musashi, NULL, ONETIME_COUNT);
  };
  
  /* AI Code Start */
  _list_item *list_item = player_list_header.next->next;
  if (list_item == player_list_header.prev) return;
  _Player *player = &list_item->data.Player;
  int tangle;
  tangle = atan3(player->x - musashi->x, player->y - musashi->y) / RM_PI;
  if (distance(musashi->x, musashi->y, player->x, player->y) < 500) {
    if(ifclockwise(musashi->angle + 90, tangle)) {
      musashi->angle -= MUSASHI_ROTATE_SPEED;
    } else {
      musashi->angle += MUSASHI_ROTATE_SPEED;
    };
    musashi->tasks.fire = true;
  } else {
    if(ifclockwise(musashi->angle, tangle)) {
      musashi->angle -= MUSASHI_ROTATE_SPEED;
    } else {
      musashi->angle += MUSASHI_ROTATE_SPEED;
    };
    musashi->dx = MUSASHI_MAX_SPEED * cos(musashi->angle * RM_PI);
    musashi->dy = MUSASHI_MAX_SPEED * sin(musashi->angle * RM_PI);
  };
  /* AI Code End */
}

task Musashi_Draw(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  /* Draw Musashi */
  int i;
  SDL_Surface *image;
  i = (optv->count / -10) % 3;
  cap_angle(&musashi->angle);
  image = Sprite_Musashi[i][musashi->angle];
  Sprite_Add(musashi->x, musashi->y, Z_ORDER_D2, image);
  
  if (musashi->shield <= MUSASHI_SHIELD * 0.5) {
    Task_Add(Smoke_Draw,
	     Smoke_Create(musashi->x, musashi->y),
	     NULL, ONETIME_COUNT + 3);
  };
  if (musashi->shield <= MUSASHI_SHIELD * 0.3) {
    Task_Add(Smoke_Draw,
	     Smoke_Create(musashi->x, musashi->y + 10),
	     NULL, ONETIME_COUNT + 7);
  };
}

task Musashi_Fire(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  int mx, my;
  
  if (optv->count == MUSASHI_FIRE_MISSILE_WAIT) {
    mx = musashi->x;
    my = musashi->y;
    Task_Add(Missile_Add, 
	     Missile_Create(mx, my, musashi->dx, musashi->dy, musashi->angle + 90),
	     NULL, ONETIME_COUNT);
    if (musashi->shield >= MUSASHI_SHIELD * 0.3) {
      mx = musashi->x + 30 * cos((musashi->angle + 180) * RM_PI);
      my = musashi->y + 30 * sin((musashi->angle + 180) * RM_PI);
      Task_Add(Missile_Add,
	       Missile_Create(mx, my, musashi->dx, musashi->dy, musashi->angle + 110),
	       NULL, ONETIME_COUNT);
      mx = musashi->x + 30 * cos(musashi->angle * RM_PI);
      my = musashi->y + 30 * sin(musashi->angle * RM_PI);
      Task_Add(Missile_Add,
	       Missile_Create(mx, my, musashi->dx, musashi->dy, musashi->angle + 70),
	       NULL, ONETIME_COUNT);
    };
  };
  if (optv->count == ONETIME_COUNT) musashi->waits.fire = false;
}

task Musashi_was_Hit(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  switch(musashi->tasks.was_hit) {
  case __hit_missile:
    Task_Add(Musashi_was_Hit_by_Missile, musashi, NULL, ONETIME_COUNT + 10);
    break;
  case __hit_flame:
    Task_Add(Musashi_was_Hit_by_Flame, musashi, NULL, ONETIME_COUNT + 10);
    break;
  case __hit_homing:
    Task_Add(Musashi_was_Hit_by_Homing, musashi, NULL, ONETIME_COUNT + 10);
    break;
  default:
    break;
  };
  musashi->tasks.was_hit = __hit_no;
}

task Musashi_was_Hit_by_Missile(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  if (optv->count == ONETIME_COUNT) {
    musashi->shield -= MISSILE_POWER;
  };
}

task Musashi_was_Hit_by_Flame(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  if (optv->count == ONETIME_COUNT) {
    musashi->shield -= FLAME_POWER;
  };
}

task Musashi_was_Hit_by_Homing(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  if (optv->count == ONETIME_COUNT) {
    musashi->shield -= HOMING_POWER;
  };
}

task Musashi_Kill(_option *optv)
{
  _Musashi *musashi;
  musashi = (_Musashi *)optv->socket[0];
  
  /* Move Musashi */
  musashi->x += musashi->dx * 0.01;
  musashi->y += musashi->dy * 0.01;
  musashi->dx *= 0.90;
  musashi->dy *= 0.90;
  cap_angle(&musashi->angle);
  
  if (optv->count == MUSASHI_KILL_WAIT) {
    Task_Add(Explosion_Draw, 
	     Explosion_Create(musashi->x, musashi->y),
	     NULL, ONETIME_COUNT + 7);
  };
  if (optv->count == MUSASHI_KILL_WAIT - 4) {
    Task_Add(Explosion_Draw,
	     Explosion_Create(musashi->x + 30, musashi->y),
	     NULL, ONETIME_COUNT + 9);
  };
  if (optv->count == MUSASHI_KILL_WAIT - 6) {
    Task_Add(Explosion_Draw,
	     Explosion_Create(musashi->x -15, musashi->y +15),
	     NULL, ONETIME_COUNT + 15);
  };
  if (optv->count == ONETIME_COUNT + 1) {
    Task_Add(Explosion_Draw,
	     Explosion_Create(musashi->x, musashi->y +15),
	     NULL, ONETIME_COUNT + 30);
  };
  
  int i;
  _list_item *list_item;
  _task_item *task_item;

  if (optv->count == ONETIME_COUNT) {
    list_item = active_task_list_header->next->next;
    while (list_item != active_task_list_header->prev) {
      task_item = &list_item->data.task_item;
      for (i = 0; i < MAX_SOCKET; i++) {
	if (task_item->option.socket[i] == musashi) {
	  if (task_item->fp == Hitcircle_Move) {
	    task_item->option.count = ONETIME_COUNT;
	  } else {
	    task_item->option.count = KILL_COUNT;
	  };
	};
      };
      list_item = list_item->next;
    };
    list_item = expired_task_list_header->next->next;
    while (list_item != expired_task_list_header->prev) {
      task_item = &list_item->data.task_item;
      for (i = 0; i < MAX_SOCKET; i++) {
	if (task_item->option.socket[i] == musashi) {
	  if (task_item->fp == Hitcircle_Move) {
	    task_item->option.count = ONETIME_COUNT;
	  } else {
	    task_item->option.count = KILL_COUNT;
	  };
	};
      };
      list_item = list_item->next;
    };
    Kill_item(&musashi_list_header, (_list_item *)musashi->root);
    pfree(musashi->root);
  };
}
